package com.yanqu.road.server.cmd;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.server.CrossChannelRouteProto;
import com.yanqu.road.server.channelserver.manager.user.ChannelUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

public abstract class CrossChannelCommand extends CrossCommand {

    protected int channelId;
    protected long userServerId;

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage basePacket) throws Exception {
        try {
            CrossChannelRouteProto.CrossChannelCommandMsg baseMsg = CrossChannelRouteProto.CrossChannelCommandMsg.parseFrom(basePacket.getBytes());
            channelId = baseMsg.getChannelId();
            userServerId = baseMsg.getServerId();
            YanQuMessage packet = YanQuMessageUtils.buildMessageByte(basePacket.getCode(), baseMsg.getMsg().toByteArray());
            packet.setPlayerId(basePacket.getPlayerId());

            if (basePacket.getPlayerId() > 0) {
                ChannelUserMgr.cacheUserChannel(basePacket.getPlayerId(), userServerId, channelId);
            }

            super.execute(ctx, packet);
        }catch (Exception e) {
            log.error("Cmd error, userId = " + basePacket.getPlayerId() + ", code = " + basePacket.getCode(), e);
        }
    }

}
